<?php

namespace app\services\setting;

use app\dao\setting\SystemMenusDao;
use app\exceptions\AdminException;
use app\exceptions\AuthException;
use app\services\BaseService;
use app\utils\Token;

class SystemMenusService extends BaseService
{

    public function __construct(SystemMenusDao $dao)
    {
        $this->dao = $dao;
    }

    public function getList(array $where)
    {
        $jwt = Token::verifyToken($where['accessToken']);
        if (!$jwt) {
            throw new AuthException('accessToken不合法', 401);
        }
        $userId = Token::getUserId($where['accessToken'], 'userId');
        $data = $this->dao->getList($where, $userId);
        if ($data) {
            foreach ($data as $key => $val) {
                $data[$key]['meta'] = [
                    'title' => $val['title'], 'icon' => $val['icon'],
                    'affix' => $val['affix'], 'badge' => $val['badge'],
                    'breadcrumb' => $val['breadcrumb'], 'remixIcon' => $val['remixIcon'],
                    'noKeepAlive' => $val['noKeepAlive']
                ];
                $data[$key]['component'] = $val['componentsPath'];
                $data[$key]['path'] = $val['routePath'];
                unset($val['title']);
                unset($val['affix']);
                unset($val['icon']);
                unset($val['badge']);
                unset($val['breadcrumb']);
                unset($val['noKeepAlive']);
                unset($val['remixIcon']);
                unset($val['componentsPath']);
                unset($val['routePath']);
            }
            return $data['menu'] = $this->create_tree_list(0, $data, 0);
        } else {
            return $data;
        }
    }

    public function getMenusList(array $where)
    {
        $data = $this->dao->getMenusList($where);
        if ($data) {
            foreach ($data as $key => $val) {
                $data[$key]['meta'] = [
                    'title' => $val['title'], 'icon' => $val['icon'],
                    'affix' => $val['affix'], 'badge' => $val['badge'],
                    'breadcrumb' => $val['breadcrumb'], 'remixIcon' => $val['remixIcon'],
                    'noKeepAlive' => $val['noKeepAlive']
                ];
                $data[$key]['component'] = $val['componentsPath'];
                $data[$key]['path'] = $val['routePath'];
                unset($val['title']);
                unset($val['affix']);
                unset($val['icon']);
                unset($val['badge']);
                unset($val['breadcrumb']);
                unset($val['noKeepAlive']);
                unset($val['remixIcon']);
                unset($val['componentsPath']);
                unset($val['routePath']);
//                unset($val['hidden']);
            }
            return $data['menu'] = $this->create_tree_list(0, $data, 0);
        } else {
            return $data;
        }
    }

    /**
     * 添加菜单
     * @param array $where
     * @return void
     */
    public function create(array $where)
    {
        return $this->dao->create($where);
    }

    /**
     * 递归实现无限极分类
     * @param $pid 父ID
     * @param $arr 递归数据
     * @param $level 分类级别
     * @return $list 分好类的数组 直接遍历即可 $level可以用来遍历缩进
     */
    public function create_tree_list($pid, $arr, $level, &$tree = [])
    {
        foreach ($arr as $key => $vo) {
            if ($vo['pid'] == $pid) {
                $vo['level'] = $level;
                $c = $this->create_tree_list($vo['id'], $arr, $level + 1);
                if ($c) {
                    $vo['children'] = $c;
                    if($vo['level'] > 0){
                        foreach ($vo['children'] as $k => $v){
                            $vo['permission'] .= $v['front'].',';
                        }
                        $vo['meta'] = ['permission' => trim($vo['permission'],',')
                            ,'title' => $vo['meta']['title']
                            ,'affix' => $vo['meta']['affix']
                            ,'icon'  => $vo['meta']['icon']
                            ,'badge'  => $vo['meta']['badge']
                            ,'breadcrumb'  => $vo['meta']['breadcrumb']
                            ,'remixIcon'  => $vo['meta']['remixIcon']
                            ,'noKeepAlive'  => $vo['meta']['noKeepAlive']
                            ];
                    }
                }
                $tree[] = $vo;

            }
        }
        return $tree;
    }

    public function update(array $where)
    {
        return $this->dao->updateDao($where);

    }

    public function delete(array $where)
    {
        return $this->dao->deleteDao($where);
    }

    public function searchTitle(array $where)
    {
        $data = $this->dao->searchTitle($where);
        if ($data) {
            foreach ($data as $key => $val) {
                $data[$key]['meta'] = [
                    'title' => $val['title'], 'icon' => $val['icon'],
                    'affix' => $val['affix'], 'badge' => $val['badge'],
                    'breadcrumb' => $val['breadcrumb'], 'remixIcon' => $val['remixIcon'],
                    'noKeepAlive' => $val['noKeepAlive']
                ];
                $data[$key]['component'] = $val['componentsPath'];
                $data[$key]['path'] = $val['routePath'];
                unset($val['title']);
                unset($val['affix']);
                unset($val['icon']);
                unset($val['badge']);
                unset($val['breadcrumb']);
                unset($val['noKeepAlive']);
                unset($val['remixIcon']);
                unset($val['componentsPath']);
                unset($val['routePath']);
//                unset($val['hidden']);
            }
            return $data['menu'] = $this->create_tree_list(0, $data, 0);
        }
    }

    public function searchPidAndNotMenu(array $where)
    {
        return $this->dao->searchPidAndNotMenu($where);
    }

}